package com.mall.service.eshop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.eshop.SaleEshopService;
import com.mall.custom.dao.read.SaleEshopReadMapper;
import com.mall.custom.dao.write.SaleEshopWriteMapper;
import com.mall.pojo.SaleEshop;
import com.mall.pojo.SaleEshopExample;
import com.mall.pojo.SaleEshopExample.Criteria;
import com.mall.query.eshop.SaleEshopQuery;

@Service
public class SaleEshopServiceImpl implements SaleEshopService {
	private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");

	@Autowired
	private SaleEshopReadMapper saleEshopReadMapper;
	@Autowired
	private SaleEshopWriteMapper saleEshopWriteMapper;

	/**
	 * 根据id查找出售的网店
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public SaleEshop getSaleEshopBySaleEshopId(Long id) {
		if(id == null || id <= 0){
			return null;
		}
		return this.saleEshopReadMapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据条件获得出售店铺集合
	 * 
	 * @param saleEshopExample
	 * @return
	 */
	@Override
	public List<SaleEshop> getSaleEshopListBySaleEshopExample(SaleEshopExample saleEshopExample) {
		return this.saleEshopReadMapper.selectByExample(saleEshopExample);
	}

	/**
	 * 插入待售店铺
	 * 
	 * @param saleEshop
	 * @return
	 */
	@Override
	public int insertSaleEshopBySaleEshop(SaleEshop saleEshop) {
		// TODO Auto-generated method stub
		return this.saleEshopWriteMapper.insert(saleEshop);
	}

	/**
	 * 修改待售店铺信息
	 * 
	 * @param saleEshop
	 * @return
	 */
	@Override
	public int modifySaleEshopBySaleEshop(SaleEshop saleEshop, SaleEshop existSaleEshop) {
		saleEshop.setGmtCreate(new Date());
		if (existSaleEshop != null) {
			saleEshop.setId(existSaleEshop.getId());
			saleEshop.setEshopNo(existSaleEshop.getEshopNo());
			saleEshop.setEshopCompanyId(existSaleEshop.getEshopCompanyId());
			saleEshop.setTrademarkId(existSaleEshop.getTrademarkId());
			saleEshop.setGmtCreate(existSaleEshop.getGmtCreate());
		}
		return this.saleEshopWriteMapper.updateByPrimaryKey(saleEshop);
	}

	/**
	 * 删除店铺
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public int deleteSaleEshopBySaleEshopId(Long id) {
		// TODO Auto-generated method stub
		return this.saleEshopWriteMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<SaleEshop> getSaleEshopListByIds(List<Long> ids) {
		// TODO Auto-generated method stub
		return this.saleEshopReadMapper.getSaleEshopListByIds(ids);
	}

	/**
	 * 店鋪編號
	 */
	public String generateEshopNo() {
		Date currentTime = new Date();
		String dateTimeStr = dateFormatter.format(currentTime);
		int random = 10000 + (int) (Math.random() * 89999);
		return dateTimeStr + random;
	}

	/**
	 * 後台展示出售的店鋪列表
	 * 
	 * @param saleEshopQuery
	 * @return
	 */
	@Override
	public List<SaleEshop> getSaleEshopListBySaleEshopQuery(SaleEshopQuery saleEshopQuery) {
		SaleEshopExample saleEshopExample = getSaleEshopExampleBySaleEshopQuery(saleEshopQuery);
		return this.saleEshopReadMapper.selectByExample(saleEshopExample);
	}
	
	private SaleEshopExample getSaleEshopExampleBySaleEshopQuery(SaleEshopQuery saleEshopQuery){
		SaleEshopExample saleEshopExample = new SaleEshopExample();
		if(saleEshopQuery.getPage() != null){
			saleEshopExample.setLimit(saleEshopQuery.getPage().getLength());
			saleEshopExample.setStart(saleEshopQuery.getPage().getBegin());
		}
		if(saleEshopQuery.getOrderby() != null && saleEshopQuery.getOrderby().equals("1")){
			saleEshopExample.setOrderByClause("price desc");
		}else if(saleEshopQuery.getOrderby() != null && saleEshopQuery.getOrderby().equals("2")){
			saleEshopExample.setOrderByClause("price asc");
		}else if(saleEshopQuery.getOrderby() != null){
			saleEshopExample.setOrderByClause(saleEshopQuery.getOrderby());
		}else if(saleEshopQuery.getOrderby() == null){
			saleEshopExample.setOrderByClause("id desc");
		}
		Criteria criteria = saleEshopExample.createCriteria();
		if (saleEshopQuery.getTruth() != null) {
			criteria.andTruthEqualTo(saleEshopQuery.getTruth());
		}
		if (saleEshopQuery.getType() != null) {
			criteria.andTypeEqualTo(saleEshopQuery.getType());
		}
		if (saleEshopQuery.getStatus() != null) {
			criteria.andStatusEqualTo(saleEshopQuery.getStatus());
		}
		if (saleEshopQuery.getSaleEshopId() != null) {
			criteria.andIdEqualTo(saleEshopQuery.getSaleEshopId());
		}
		
		if (StringUtils.isNotBlank(saleEshopQuery.getKeyWord())) {
			criteria.andPyNameLike("%" + saleEshopQuery.getKeyWord() + "%");
		}
		if(saleEshopQuery.getQualification() != null){
			if(saleEshopQuery.getQualification() == 1){
				criteria.andQualificationLike("旗舰店");
			}else if(saleEshopQuery.getQualification() == 2){
				criteria.andQualificationLike("专营店");
			}else if(saleEshopQuery.getQualification() == 3){
				criteria.andQualificationLike("专卖店");
			}
		}
		
		if(saleEshopQuery.getIndustry() != null){
			if(saleEshopQuery.getIndustry() == 1){
				criteria.andIndustryLike("服饰鞋包");
			}else if(saleEshopQuery.getIndustry() == 2){
				criteria.andIndustryLike("美容护理");
			}else if(saleEshopQuery.getIndustry() == 3){
				criteria.andIndustryLike("数码电器");
			}else if(saleEshopQuery.getIndustry() == 4){
				criteria.andIndustryLike("母婴用品");
			}else if(saleEshopQuery.getIndustry() == 5){
				criteria.andIndustryLike("食品/保健");
			}else if(saleEshopQuery.getIndustry() == 6){
				criteria.andIndustryLike("运动户外");
			}else if(saleEshopQuery.getIndustry() == 7){
				criteria.andIndustryLike("家居用品");
			}else if(saleEshopQuery.getIndustry() == 8){
				criteria.andIndustryLike("家装家饰");
			}else if(saleEshopQuery.getIndustry() == 9){
				criteria.andIndustryLike("游戏/话费");
			}else if(saleEshopQuery.getIndustry() == 10){
				criteria.andIndustryLike("其他行业");
			}else if(saleEshopQuery.getIndustry() == 11){
				criteria.andIndustryLike("生活服务");
			}else if(saleEshopQuery.getIndustry() == 12){
				criteria.andIndustryLike("汽车配件");
			}else if(saleEshopQuery.getIndustry() == 13){
				criteria.andIndustryLike("书籍音像");
			}else if(saleEshopQuery.getIndustry() == 14){
				criteria.andIndustryLike("珠宝/首饰");
			}else if(saleEshopQuery.getIndustry() == 15){
				criteria.andIndustryLike("玩乐/收藏");
			}
		}
		
		if(saleEshopQuery.getTaxpayer() != null){
			if(saleEshopQuery.getTaxpayer() == 1){
				criteria.andTaxpayerTypeLike("一般纳税人");
			}else if(saleEshopQuery.getTaxpayer() == 2){
				criteria.andTaxpayerTypeLike("小规模纳税人");
			}
		}
		
		if(saleEshopQuery.getTrademark() != null){
			if(saleEshopQuery.getTrademark() == 1){
				criteria.andTrademarkTypeLike("R标%");
			}else if(saleEshopQuery.getTrademark() == 2){
				criteria.andTrademarkTypeLike("TM标%");
			}
		}
		
		if(saleEshopQuery.getPrice() != null){
			if(saleEshopQuery.getPrice() == 1){
				criteria.andPriceBetween(50000, 100000);
			}else if(saleEshopQuery.getPrice() == 2){
				criteria.andPriceBetween(100000, 200000);
			}else if(saleEshopQuery.getPrice() == 3){
				criteria.andPriceBetween(200000, 500000);
			}else if(saleEshopQuery.getPrice() == 4){
				criteria.andPriceGreaterThan(500000);
			}
		}
		
		return saleEshopExample;
	}

	@Override
	public List<SaleEshop> getSaleEshopListByQuery(SaleEshopQuery saleEshopQuery) {
		SaleEshopExample saleEshopExample = new SaleEshopExample();
		saleEshopExample = this.getSaleEshopExampleBySaleEshopQuery(saleEshopQuery);
		return this.saleEshopReadMapper.selectByExample(saleEshopExample);
	}

	@Override
	public int getSaleEshopCountByQuery(SaleEshopQuery saleEshopQuery) {
		SaleEshopExample saleEshopExample = new SaleEshopExample();
		saleEshopExample = this.getSaleEshopExampleBySaleEshopQuery(saleEshopQuery);
		return this.saleEshopReadMapper.countByExample(saleEshopExample);
	}
}
